
package system.base;

import framework.core.*;
import system.data.*;
/*
	Author    :Wing
	CreateTime:2008-02-20 17:49:49
*/
public class InSubSrv extends IHandler
{

	public InSubSrv() 
	{
		super();
	}
	private void deleteInSub(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		InSub obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = InSub.fetch(con,ids[i]);
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveInSub(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String id = context.getString("Id",null);
		InSub obj;
		if((id==null)||(id.equals("")))   //add
		{
			obj = InSub.fetch();
    	}
    	else                     //update
    	{
    		obj = InSub.fetch(con,id);
    	}
		InSubPageData data = (InSubPageData)PageData.fromForm(context,InSubPageData.class);
		context.setValue("InSubPageData",data);
		obj.fill(data);
		obj.save(con);
	}
	private void listInSub(DBConnection con,SrvContext context,String path) throws Exception
	{
		InSubSearchEvent event = (InSubSearchEvent)SearchEvent.fromForm(context,InSubSearchEvent.class);
		context.setSessionValue("InSubSearchEvent",event);
		context.setNextUrl(path+"InSubList.jsp");
	}
	private void backInSub(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"InSubList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listInSub"))
		{
			listInSub(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewInSub"))
		{
			String id = context.getString("Id",null);
			InSub obj = InSub.fetch(con,id);
			context.setValue("InSub",obj);
			context.setNextUrl(path+"InSubView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteInSub"))
		{
			context.setErrorUrl(path+"InSubList.jsp");
			deleteInSub(con,context);
			backInSub(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddInSub"))
		{
			InSub obj = InSub.fetch();
			InSubPageData data = (InSubPageData)PageData.fromTable(obj,InSubPageData.class);
			context.setValue("InSubPageData",data);
			context.setNextUrl(path+"InSubEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateInSub"))
		{
			String id = context.getString("Id",null);
			InSub obj = InSub.fetch(con,id);
			InSubPageData data = (InSubPageData)PageData.fromTable(obj,InSubPageData.class);
			context.setValue("InSubPageData",data);
			context.setNextUrl(path+"InSubEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyInSub"))
		{
			String id = context.getString("Id",null);
			InSub obj = InSub.copy(InSub.fetch(con,id));
			InSubPageData data = (InSubPageData)PageData.fromTable(obj,InSubPageData.class);
			context.setValue("InSubPageData",data);
			context.setNextUrl(path+"InSubEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveInSub"))
		{
			context.setErrorUrl(path+"InSubEdit.jsp");
			saveInSub(con,context,loginContext);
			backInSub(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewInSub"))
		{
			context.setErrorUrl(path+"InSubEdit.jsp");
			saveInSub(con,context,loginContext);
			InSub obj = InSub.fetch();
			InSubPageData data = (InSubPageData)PageData.fromTable(obj,InSubPageData.class);
			context.setValue("InSubPageData",data);
			context.setNextUrl(path+"InSubEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListInSub"))
		{
			backInSub(con,context,path);
			return;
		}
		
		//add by Wing start

		//add by Wing end

		Logger.throwException("InSubSrv","unknown action_code="+action_code);
	}
}
